package com.xinchuang.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ArrayUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xinchuang.comment.KeyWord;
import com.xinchuang.comment.R;
import com.xinchuang.domain.entity.TestModelParam;
import com.xinchuang.domain.vo.MoldSelectionVo;
import com.xinchuang.entity.*;
import com.xinchuang.exception.MyCustomException;
import com.xinchuang.mapper.*;
import com.xinchuang.service.*;
import com.xinchuang.utils.IdUtils;
import com.xinchuang.utils.MyUtils;
import com.xinchuang.utils.ObjectUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedInputStream;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class MoldSelectionsModServiceImpl implements MoldSelectionsModService {

    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Resource
    private IMoldSelectionService moldSelectionService;
    @Resource
    private IMoldSelectionDetailService moldSelectionDetailService;
    /**
     * 内部报价service
     */
    @Autowired
    private InnerQuotationMapper innerQuotationMapper;
    /**
     * 内部报价详情service
     */
    @Autowired
    private InnerQuotationDetailMapper innerQuotationDetailMapper;
    /**
     * 内部报价价格表Service接口
     */
    @Autowired
    private InnerQuotationPartPriceMapper innerQuotationPartPriceMapper;
    @Autowired
    private PartPriceMapper partPriceMapper;

    @Autowired
    private CommonQuotationMapper commonQuotationMapper;
    @Autowired
    private CommonQuotationDetailMapper commonQuotationDetailMapper;
    @Autowired
    private CommonQuotationPriceMapper commonQuotationPriceMapper;
    @Autowired
    private DfQuotationMapper dfQuotationMapper;
    @Autowired
    private DfQuotationDetailMapper dfQuotationDetailMapper;
    @Autowired
    private DfQuotationPriceMapper dfQuotationPriceMapper;
    /**
     * 吉利service
     */
    @Resource
    private JlQuotationMapper jlQuotationMapper;
    /**
     * 吉利 priceService
     */
    @Resource
    private JlQuotationPriceMapper jlQuotationPriceMapper;
    /**
     * 吉利 priceService
     */
    @Resource
    private JlQuotationDetailMapper jlQuotationDetailMapper;
    @Autowired
    private QrQuotationMapper qrQuotationMapper;
    @Autowired
    private QrQuotationDetailMapper qrQuotationDetailMapper;
    @Autowired
    private QrQuotationPriceMapper qrQuotationPriceMapper;
    @Autowired
    private BydQuotationMapper bydQuotationMapper;
    @Autowired
    private BydQuotationDetailMapper bydQuotationDetailMapper;
    @Autowired
    private BydQuotationPriceMapper bydQuotationPriceMapper;

    @Autowired
    private InnerDesignChangesMapper innerDesignChangesMapper;

    @Autowired
    private InnerDesignChangePriceMapper innerDesignChangePriceMapper;

    @Autowired
    private InnerDesignChangesDetailMapper innerDesignChangesDetailMapper;

    @Autowired
    private OuterDesignChangesMapper outerDesignChangesMapper;

    @Autowired
    private OuterDesignChangePriceMapper outerDesignChangePriceMapper;

    @Autowired
    private OuterDesignChangesDetailMapper outerDesignChangesDetailMapper;

    @Autowired
    private EdtionMapper edtionMapper;

    @Autowired
    private KeyWord keyWord;
    @Autowired
    private DictTableMapper dictTableMapper;
    @Autowired
    private TestModelParamMapper testModelParamMapper;
    @Autowired
    private MoldSelectionMapper moldSelectionMapper;
    @Autowired
    private TryOutDeviceModMapper tryOutDeviceModMapper;
    @Autowired
    private MoldSelectionDetailMapper moldSelectionDetailMapper;


    @Override
    public String getMoldSelectionList(JSONObject jsonObject) {
        //jsonobject解析获取项目名称
        String projectName = jsonObject.getString("projectName");
        //jsonobject解析获取零件厂Id（量产地）
        String partsStorageBinId = jsonObject.getString("partsStorageBinId");
        //jsonobject解析获取主机厂Id (客户)
        String mainEnginePlantId = jsonObject.getString("mainEnginePlantId");
        //jsonobject解析获取页码
        String pageNum = jsonObject.getString("pageNum");
        //数量
        String pageSize = jsonObject.getString("pageSize");
        //jsonobject解析获取备用参数1
        String param1 = jsonObject.getString("param1");

        Page<MoldSelection> moldSelectionPage = new Page<>(Integer.parseInt(pageNum), 10);
        LambdaQueryWrapper<MoldSelection> moldSelectionLambdaQueryWrapper = new LambdaQueryWrapper<>();
        moldSelectionLambdaQueryWrapper.like(StringUtils.isNotEmpty(projectName), MoldSelection::getProjectName, projectName);
        moldSelectionLambdaQueryWrapper.eq(StringUtils.isNotEmpty(partsStorageBinId), MoldSelection::getPartsStorageBinId, partsStorageBinId);
        moldSelectionLambdaQueryWrapper.eq(StringUtils.isNotEmpty(mainEnginePlantId), MoldSelection::getMainEnginePlantId, mainEnginePlantId);
        moldSelectionLambdaQueryWrapper.orderByDesc(MoldSelection::getCreateTime);
        HashMap<String, Object> result = new HashMap<>();
        Page<MoldSelection> page = moldSelectionService.page(moldSelectionPage, moldSelectionLambdaQueryWrapper);
        if (CollectionUtil.isNotEmpty(page.getRecords())) {
            result.put("dataList", page.getRecords());
        } else {
            result.put("dataList", new JSONArray());
        }
        result.put("totalNum", page.getTotal());
        return JSON.toJSONString(R.data(result));
    }

    @Override
    @Transactional
    public String imoprtFile(MultipartFile file, HttpServletRequest request) {
        try {
            MoldSelectReadListener moldSelectReadListener = new MoldSelectReadListener(moldSelectionMapper,
                    tryOutDeviceModMapper, moldSelectionDetailMapper,
                    MyUtils.getName(MyUtils.getHeader(request).getId()),
                    dictTableMapper, file, keyWord, testModelParamMapper);
            EasyExcel.read(new BufferedInputStream(file.getInputStream()), MoldSelectionDetail.class, moldSelectReadListener)
                    //表头缘故 从第5行开始读取
                    .headRowNumber(4)
                    // 读取全部sheet
                    .doReadAll();

            //校验excel本身是否存在重复的客户模具编号
            moldSelectReadListener.validHasDuplicateMoldId();

            //入库
            moldSelectReadListener.saveTestModelParam();
            moldSelectReadListener.saveMoldSelection();
            moldSelectReadListener.saveMoldSelectionDetail();

            return JSON.toJSONString(R.success("上传成功"));
        } catch (MyCustomException e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return JSON.toJSONString(R.fail(e.getMessage()));
        } catch (Exception e) {
            log.error("解析选模excel失败:{}", e.toString(), e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }

        return JSON.toJSONString(R.fail("上传失败"));
    }

    @Override
    public String deleteMoldSelections(JSONObject jsonObject) {
        //jsonobject解析获取选模表ids
        JSONArray moldSelectionIds = jsonObject.getJSONArray("moldSelectionIds");
        if (CollectionUtils.isEmpty(moldSelectionIds)) {
            return JSON.toJSONString(R.success("删除成功"));
        }

        boolean b = moldSelectionService.removeBatchByIds(moldSelectionIds);

        if (b) {
            List<BydQuotation> bydQuotations = bydQuotationMapper.selectList(new QueryWrapper<BydQuotation>().lambda().in(BydQuotation::getMoldSelectionId, moldSelectionIds));
            if (!CollectionUtils.isEmpty(bydQuotations)) {
                List<String> bydQuotationIds = bydQuotations.stream().map(bydQuotation -> bydQuotation.getId()).collect(Collectors.toList());
                bydQuotationDetailMapper.delete(new QueryWrapper<BydQuotationDetail>().lambda().in(BydQuotationDetail::getQuotationId, bydQuotationIds));
            }
            bydQuotationPriceMapper.delete(new QueryWrapper<BydQuotationPrice>().lambda().in(BydQuotationPrice::getMoldSelectionId, moldSelectionIds));
            bydQuotationMapper.delete(new QueryWrapper<BydQuotation>().lambda().in(BydQuotation::getMoldSelectionId, moldSelectionIds));

            List<CommonQuotation> commonQuotations = commonQuotationMapper.selectList(new QueryWrapper<CommonQuotation>().lambda().in(CommonQuotation::getMoldSelectionId, moldSelectionIds));
            if (!CollectionUtils.isEmpty(commonQuotations)) {
                List<String> commonQuotationIds = commonQuotations.stream().map(commonQuotation -> commonQuotation.getId()).collect(Collectors.toList());
                commonQuotationDetailMapper.delete(new QueryWrapper<CommonQuotationDetail>().lambda().in(CommonQuotationDetail::getCommonQuotationId, commonQuotationIds));
            }
            commonQuotationPriceMapper.delete(new QueryWrapper<CommonQuotationPrice>().lambda().in(CommonQuotationPrice::getMoldSelectionId, moldSelectionIds));
            commonQuotationMapper.delete(new QueryWrapper<CommonQuotation>().lambda().in(CommonQuotation::getMoldSelectionId, moldSelectionIds));

            List<DfQuotation> dfQuotations = dfQuotationMapper.selectList(new QueryWrapper<DfQuotation>().lambda().in(DfQuotation::getMoldSelectionId, moldSelectionIds));
            if (!CollectionUtils.isEmpty(dfQuotations)) {
                List<String> dfQuotationIds = dfQuotations.stream().map(dfQuotation -> dfQuotation.getId()).collect(Collectors.toList());
                dfQuotationDetailMapper.delete(new QueryWrapper<DfQuotationDetail>().lambda().in(DfQuotationDetail::getQuotationId, dfQuotationIds));
            }
            dfQuotationPriceMapper.delete(new QueryWrapper<DfQuotationPrice>().lambda().in(DfQuotationPrice::getMoldSelectionId, moldSelectionIds));
            dfQuotationMapper.delete(new QueryWrapper<DfQuotation>().lambda().in(DfQuotation::getMoldSelectionId, moldSelectionIds));

            innerDesignChangePriceMapper.delete(new LambdaQueryWrapper<InnerDesignChangePrice>().in(InnerDesignChangePrice::getMoldSelectionId, moldSelectionIds));
            innerDesignChangesDetailMapper.delete(new LambdaQueryWrapper<InnerDesignChangesDetail>().in(InnerDesignChangesDetail::getMoldSelectionId, moldSelectionIds));
            innerDesignChangesMapper.delete(new LambdaQueryWrapper<InnerDesignChanges>().in(InnerDesignChanges::getMoldSelectionId, moldSelectionIds));

            List<MoldSelectionDetail> moldSelectionDetails = moldSelectionDetailMapper.selectList(new LambdaQueryWrapper<MoldSelectionDetail>().in(MoldSelectionDetail::getMoldSelectionId, moldSelectionIds));
            if (!CollectionUtils.isEmpty(moldSelectionDetails)) {
                List<String> moldIds = moldSelectionDetails.stream().map(moldSelectionDetail -> moldSelectionDetail.getMoldId()).collect(Collectors.toList());
                innerQuotationPartPriceMapper.delete(new LambdaQueryWrapper<InnerQuotationPartPrice>().in(InnerQuotationPartPrice::getMoldId, moldIds));
                partPriceMapper.delete(new QueryWrapper<PartPrice>().lambda().in(PartPrice::getMoldId, moldIds));
                testModelParamMapper.delete(new LambdaUpdateWrapper<TestModelParam>().in(TestModelParam::getCustomerMoldId, moldIds));
                edtionMapper.delete(new LambdaUpdateWrapper<Edtion>().in(Edtion::getRed_id, moldIds));
            }
            innerQuotationDetailMapper.delete(new LambdaQueryWrapper<InnerQuotationDetail>().in(InnerQuotationDetail::getMoldSelectionId, moldSelectionIds));
            innerQuotationMapper.delete(new LambdaQueryWrapper<InnerQuotation>().in(InnerQuotation::getMoldSelectionId, moldSelectionIds));

            jlQuotationDetailMapper.delete(new QueryWrapper<JlQuotationDetail>().lambda().in(JlQuotationDetail::getMoldSelectionId, moldSelectionIds));
            jlQuotationMapper.delete(new QueryWrapper<JlQuotation>().lambda().in(JlQuotation::getMoldSelectionId, moldSelectionIds));
            jlQuotationPriceMapper.delete(new QueryWrapper<JlQuotationPrice>().lambda().in(JlQuotationPrice::getMoldSelectionId, moldSelectionIds));

            outerDesignChangePriceMapper.delete(new LambdaQueryWrapper<OuterDesignChangePrice>().in(OuterDesignChangePrice::getMoldSelectionId, moldSelectionIds));
            outerDesignChangesDetailMapper.delete(new LambdaQueryWrapper<OuterDesignChangesDetail>().in(OuterDesignChangesDetail::getMoldSelectionId, moldSelectionIds));
            outerDesignChangesMapper.delete(new LambdaQueryWrapper<OuterDesignChanges>().in(OuterDesignChanges::getMoldSelectionId, moldSelectionIds));

            qrQuotationDetailMapper.delete(new QueryWrapper<QrQuotationDetail>().lambda().in(QrQuotationDetail::getMoldSelectionId, moldSelectionIds));
            qrQuotationMapper.delete(new QueryWrapper<QrQuotation>().lambda().in(QrQuotation::getMoldSelectionId, moldSelectionIds));
            qrQuotationPriceMapper.delete(new QueryWrapper<QrQuotationPrice>().lambda().in(QrQuotationPrice::getMoldSelectionId, moldSelectionIds));


            moldSelectionDetailService.remove(new LambdaQueryWrapper<MoldSelectionDetail>().in(MoldSelectionDetail::getMoldSelectionId, moldSelectionIds));
            return JSON.toJSONString(R.success("删除成功"));
        }
        return JSON.toJSONString(R.fail("参数错误，删除失败"));
    }

    @Override
    public String saveMoldSelection(JSONObject jsonObject) {
        MoldSelection moldSelection = JSONObject.parseObject(String.valueOf(jsonObject), MoldSelection.class);
        moldSelection.setMainEnginePlantId(jsonObject.getString("customerId"));
        String moldSelectionId = moldSelection.getMoldSelectionId();
        if (StringUtils.isEmpty(moldSelectionId)) {
            moldSelection.setMoldSelectionId(IdUtils.simpleUUID());
        }
        moldSelectionService.saveOrUpdate(moldSelection);
        //jsonobject解析获取数据列表
        JSONArray dataList = jsonObject.getJSONArray("dataList");
        for (int i = 0; i < dataList.size(); i++) {
            //获取dataList的对象
            JSONObject targetObject = dataList.getJSONObject(i);
            MoldSelectionDetail moldSelectionDetail = new MoldSelectionDetail();

            //获取dataList中的subTableId值
            String subTableId = targetObject.getString("subTableId");
            if (StringUtils.isEmpty(subTableId)) {
                moldSelectionDetail.setId(IdUtils.simpleUUID());
            } else {
                moldSelectionDetail.setId(subTableId);
            }
            //获取dataList中的moldInfo对象
            JSONObject baseInfo = targetObject.getJSONObject("baseInfo");
            //获取需要保存的字段值
            //baseInfo
            moldSelectionDetail.setProductCode(baseInfo.getString("codeOfProduct"));
            moldSelectionDetail.setProductName(baseInfo.getString("nameOfProduct"));
            moldSelectionDetail.setProductImage(baseInfo.getString("imageOfProduct"));
            moldSelectionDetail.setProductHeight(baseInfo.getString("heightOfProduct"));
            moldSelectionDetail.setProductLength(baseInfo.getString("lengthOfProduct"));
            moldSelectionDetail.setProductWidth(baseInfo.getString("widthOfProduct"));
            moldSelectionDetail.setMaterial(baseInfo.getString("materials"));
            moldSelectionDetail.setSurface(baseInfo.getString("surfaceRequire"));
            //moldInfo
            //获取dataList中的moldInfo对象
            JSONObject moldInfo = targetObject.getJSONObject("moldInfo");
            moldSelectionDetail.setMoldId(moldInfo.getString("customerMoldId"));
            moldSelectionDetail.setCavity(moldInfo.getString("materialOfCavity"));
            moldSelectionDetail.setCore(moldInfo.getString("materialOfCore"));
            moldSelectionDetail.setCavityNum(moldInfo.getString("perCavitiesOfMold"));
            moldSelectionDetail.setLongevity(moldInfo.getString("lifetimeOfMold"));
            moldSelectionDetail.setPour(moldInfo.getString("pourSystem"));
            moldSelectionDetail.setChangeColor(moldInfo.getString("isChangeColor"));
            moldSelectionDetail.setMachineSize(moldInfo.getString("sizeOfMachine"));
            moldSelectionDetail.setMoldingCycle(moldInfo.getString("moldingCycle"));
            moldSelectionDetail.setPickUpWay(moldInfo.getString("pickUpWay"));
            moldSelectionDetail.setEjectionOrresetMode(moldInfo.getString("ejectionOrresetMode"));
            moldSelectionDetail.setT0Time(moldInfo.getString("plannedT0Time"));
            moldSelectionDetail.setMoldLength(moldInfo.getString("lengthOfMold"));
            moldSelectionDetail.setMoldWidth(moldInfo.getString("widthOfMold"));
            moldSelectionDetail.setMoldHeight(moldInfo.getString("heightOfMold"));
            moldSelectionDetail.setMoldWeight(moldInfo.getString("weightOfMold"));
            moldSelectionDetail.setMoldType(moldInfo.getString("moldSelecttionType"));
            moldSelectionDetail.setIsSupply(moldInfo.getString("isSupply"));

            moldSelectionDetail.setMoldSelectionId(moldSelection.getMoldSelectionId());
            moldSelectionDetailService.saveOrUpdate(moldSelectionDetail);
        }
        return JSON.toJSONString(R.success("保存成功"));
    }

    @Override
    public String getMoldSelectionDetail(JSONObject jsonObject) {
        //jsonobject解析获取选模表Id
        String moldSelectionId = jsonObject.getString("moldSelectionId");

        LambdaQueryWrapper<MoldSelection> moldSelectionLambdaQueryWrapper = new LambdaQueryWrapper<>();
        moldSelectionLambdaQueryWrapper.eq(StringUtils.isNotEmpty(moldSelectionId), MoldSelection::getMoldSelectionId, moldSelectionId);
        MoldSelection moldSelection = moldSelectionService.getOne(moldSelectionLambdaQueryWrapper);
        if (ObjectUtil.isEmpty(moldSelection)) {
            return JSON.toJSONString(R.fail("无数据"));
        }

        LambdaQueryWrapper<MoldSelectionDetail> moldSelectionDetailQueryWrapper = new LambdaQueryWrapper<>();
        moldSelectionDetailQueryWrapper.eq(StringUtils.isNotEmpty(moldSelection.getMoldSelectionId()), MoldSelectionDetail::getMoldSelectionId, moldSelection.getMoldSelectionId()).orderByAsc(MoldSelectionDetail::getId, MoldSelectionDetail::getMoldId);
        List<MoldSelectionDetail> moldSelectionDetaillist = moldSelectionDetailService.list(moldSelectionDetailQueryWrapper);
        MoldSelectionVo vo = new MoldSelectionVo();
        BeanUtils.copyProperties(moldSelection, vo);
        vo.setCheckoutName(moldSelection.getAuditor());
        JSONObject object = JSONObject.parseObject(JSON.toJSONString(vo));
        JSONArray jsonArray = new JSONArray();
        for (MoldSelectionDetail moldSelectionDetail : moldSelectionDetaillist) {
            JSONObject obj = new JSONObject();
            JSONObject baseInfo = getBaseInfoObject(moldSelectionDetail);
            JSONObject moldInfo = getMoldInfoObject(moldSelectionDetail);
            obj.put("baseInfo", baseInfo);
            obj.put("moldInfo", moldInfo);
            obj.put("subTableId", moldSelectionDetail.getId());
            jsonArray.add(obj);
        }
        object.put("dataList", jsonArray);

        return JSON.toJSONString(R.data(object));
    }

    private static JSONObject getBaseInfoObject(MoldSelectionDetail moldSelectionDetail) {
        JSONObject baseInfo = new JSONObject();
        baseInfo.put("nameOfProduct", moldSelectionDetail.getProductName());
        baseInfo.put("codeOfProduct", moldSelectionDetail.getProductCode());
        baseInfo.put("imageOfProduct", moldSelectionDetail.getProductImage());
        baseInfo.put("heightOfProduct", moldSelectionDetail.getProductHeight());
        baseInfo.put("lengthOfProduct", moldSelectionDetail.getProductLength());
        baseInfo.put("widthOfProduct", moldSelectionDetail.getProductWidth());
        baseInfo.put("materials", moldSelectionDetail.getMaterial());
        baseInfo.put("surfaceRequire", moldSelectionDetail.getSurface());
        return baseInfo;
    }

    private static JSONObject getMoldInfoObject(MoldSelectionDetail moldSelectionDetail) {
        JSONObject moldInfo = new JSONObject();
        moldInfo.put("customerMoldId", moldSelectionDetail.getMoldId());
        moldInfo.put("materialOfCavity", moldSelectionDetail.getCavity());
        moldInfo.put("materialOfCore", moldSelectionDetail.getCore());
        moldInfo.put("perCavitiesOfMold", moldSelectionDetail.getCavityNum());
        moldInfo.put("lifetimeOfMold", moldSelectionDetail.getLongevity());
        moldInfo.put("pourSystem", moldSelectionDetail.getPour());
        moldInfo.put("isChangeColor", moldSelectionDetail.getChangeColor());
        moldInfo.put("sizeOfMachine", moldSelectionDetail.getMachineSize());
        moldInfo.put("moldingCycle", moldSelectionDetail.getMoldingCycle());
        moldInfo.put("pickUpWay", moldSelectionDetail.getPickUpWay());
        moldInfo.put("ejectionOrresetMode", moldSelectionDetail.getEjectionOrresetMode());
        moldInfo.put("plannedT0Time", moldSelectionDetail.getT0Time());
        moldInfo.put("lengthOfMold", moldSelectionDetail.getMoldLength());
        moldInfo.put("widthOfMold", moldSelectionDetail.getMoldWidth());
        moldInfo.put("heightOfMold", moldSelectionDetail.getMoldHeight());
        moldInfo.put("weightOfMold", moldSelectionDetail.getMoldWeight());
        moldInfo.put("moldSelecttionType", moldSelectionDetail.getMoldType());
        moldInfo.put("isSupply", moldSelectionDetail.getIsSupply());
        return moldInfo;
    }

    @Override
    public String postToCheck(JSONObject jsonObject) {
        //jsonobject解析获取选模表Id
        String moldSelectionId = jsonObject.getString("moldSelectionId");
        if (StringUtils.isEmpty(moldSelectionId)) {
            return JSON.toJSONString(R.fail("选模表ID不能为空"));
        }
        LambdaQueryWrapper<MoldSelection> moldSelectionLambdaQueryWrapper = new LambdaQueryWrapper<>();
        moldSelectionLambdaQueryWrapper.eq(StringUtils.isNotEmpty(moldSelectionId), MoldSelection::getMoldSelectionId, moldSelectionId);
        MoldSelection moldSelection = moldSelectionService.getOne(moldSelectionLambdaQueryWrapper);
        if (ObjectUtil.isEmpty(moldSelection)) {
            return JSON.toJSONString(R.fail("参数错误"));
        }
        LambdaUpdateWrapper<MoldSelection> moldSelectionUpdateWrapper = new LambdaUpdateWrapper<>();
        moldSelectionUpdateWrapper
                .eq(MoldSelection::getMoldSelectionId, moldSelectionId)
                .set(MoldSelection::getStatus, "1");

        moldSelectionService.update(moldSelectionUpdateWrapper);
        return JSON.toJSONString(R.data(new MoldSelection("1")));
    }

    @Override
    public String deleteSubTableById(JSONObject jsonObject) {
        //jsonobject解析获取分表Id
        String subTableId = jsonObject.getString("subTableId");
        //jsonobject解析获取选模表Id
        String moldSelectionId = jsonObject.getString("moldSelectionId");
        if (StringUtils.isEmpty(subTableId) && StringUtils.isEmpty(moldSelectionId)) {
            return JSON.toJSONString(R.fail("选模表ID不能为空，分表ID不能为空 "));
        }
        LambdaQueryWrapper<MoldSelectionDetail> moldSelectionDetailQueryWrapper = new LambdaQueryWrapper<>();
        moldSelectionDetailQueryWrapper.eq(StringUtils.isNotEmpty(moldSelectionId), MoldSelectionDetail::getMoldSelectionId, moldSelectionId);
        moldSelectionDetailQueryWrapper.eq(StringUtils.isNotEmpty(subTableId), MoldSelectionDetail::getId, subTableId);
        MoldSelectionDetail moldSelectionDetail = moldSelectionDetailService.getOne(moldSelectionDetailQueryWrapper);
        if (ObjectUtil.isEmpty(moldSelectionDetail)) {
            return JSON.toJSONString(R.fail("参数错误"));
        }
        String moldId = moldSelectionDetail.getMoldId();
        int delete = testModelParamMapper.delete(new LambdaUpdateWrapper<TestModelParam>().eq(TestModelParam::getCustomerMoldId, moldId));

//        partPriceService.remove(new LambdaQueryWrapper<PartPrice>().eq(PartPrice::getMoldId, moldId));
//        iInnerQuotationPartPriceService.remove(new LambdaQueryWrapper<InnerQuotationPartPrice>().eq(InnerQuotationPartPrice::getMoldId, moldId));
//        iInnerQuotationDetailService.remove(new LambdaQueryWrapper<InnerQuotationDetail>().eq(InnerQuotationDetail::getMoldId, moldId));

//        CommonQuotation one = commonQuotationMapper.selectOne(new QueryWrapper<CommonQuotation>().lambda().eq(CommonQuotation::getMoldSelectionId, moldSelectionId));
//        if (null != one) {
//            String quotationId = one.getId();
//            List<CommonQuotationDetail> list = commonQuotationDetailService.list(new QueryWrapper<CommonQuotationDetail>().lambda()
//                    .ne(CommonQuotationDetail::getProductCode, moldId)
//                    .eq(CommonQuotationDetail::getCommonQuotationId, quotationId));
//            //求和统计
//            double textureFee = list.stream().map(param -> param.getTextureFee()).filter(a -> a != null).mapToDouble(Double::parseDouble).sum();
//            double moldFee = list.stream().map(param -> param.getMoldFee()).filter(a -> a != null).mapToDouble(Double::parseDouble).sum();
//            double priceWithoutTax = list.stream().map(param -> param.getPriceWithoutTax()).filter(a -> a != null).mapToDouble(Double::parseDouble).sum();
//            one.setTextureFee(String.valueOf(textureFee));
//            one.setMoldFee(String.valueOf(moldFee));
//            one.setPriceWithoutTax(String.valueOf(priceWithoutTax));
//            commonQuotationMapper.updateById(one);
//        }
//        commonQuotationDetailService.remove(new QueryWrapper<CommonQuotationDetail>().lambda().eq(CommonQuotationDetail::getId, subTableId));
//        commonQuotationPriceService.remove(new QueryWrapper<CommonQuotationPrice>().lambda()
//                .eq(CommonQuotationPrice::getDetailId, subTableId)
//                .eq(CommonQuotationPrice::getMoldSelectionId, moldSelectionId));

        moldSelectionDetailService.removeById(moldSelectionDetail);

        return JSON.toJSONString(R.success("删除成功"));
    }

}
